import Taro from '@tarojs/taro'
import style from './style.less'

const CargoSource = ({ info }: any) => {
  
  const handleGrab = (cargoId) => {
    // 未认证或未登录
    if (!Taro.getStorageSync('auth')) {
      Taro.showModal({
        title: '提示',
        content: Taro.getStorageSync('token') ? '您还未认证，点击确定去到认证页面' : '您还未登录，点击确定去到登录页面',
        success: function (res) {
          if (res.confirm) {
            ok()
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })
      return
    }
    // 认证用户
    Taro.navigateTo({ url: `/packageB/details/index?cargoId=${cargoId}` })
  }

  const ok = () => {
    if (Taro.getStorageSync('token')) {
      Taro.navigateTo({
        url: '/packageA/authentication/index'
      })
    } else {
      Taro.navigateTo({
        url: '/packageA/login/index'
      })
    }
  }

  return (
    <div className={style.root}>
      <div className="zhuang">装</div>
      <div className="start">{info.loadAddress.substring(info.loadAddress.indexOf('-') + 1, info.loadAddress.indexOf(' '))}<i className={info.buyoutPrice ? 'i2' : 'i1'}>{info.buyoutPrice ? '冷链货源' : '优选货源'}</i></div>
      <div className="xie">卸</div>
      <div className="end">{info.unloadingAdress.substring(info.unloadingAdress.indexOf('-') + 1, info.unloadingAdress.indexOf(' '))}</div>
      <div className="type">{info.truckLength}米 ｜ {info.truckType} ｜ {info.truckWeight}吨 ｜ {info.cargoCapacipy}方 ｜ {info.cargoName}</div>
      <div className="distance">总里程：{info.mileage}公里 ｜ 订金：{info.deposit}元({info.needReturnDeposit ? '退还' : '不退还'})</div>
      <div className="time">装货时间：{info.loadTime}</div>
      <div className="money"><i>¥</i>{info.cargoFee}</div>
      <div className="grab" onClick={() => { handleGrab(info.cargoId) }}>去抢单</div>
    </div>
  )
}

export default CargoSource